//
//  SDHSportsSubNewsVC.m
//  SDHSportsNews
//
//  Created by MichaelShan on 2021/3/7.
//

#import "SDHSportsSubNewsVC.h"
#import "SDHSportsHomeNewsCell.h"
#import "SDHSportsTeamInfoVC.h"
#import "SDHSportsDataManager.h"
#import "AFNetworking.h"
#import "SDHSportsRequest.h"
#import "SDHSportsNetRequest.h"
#import "SDHSportsNewsModal.h"
#import "MJRefresh.h"
#import "MBProgressHUD.h"

@interface SDHSportsSubNewsVC () <UITableViewDelegate, UITableViewDataSource> {
    BOOL isRefresh;
    NSInteger pageNum;
    NSInteger contentType;
}

@property (nonatomic, strong) UITableView *newsTable;
@property (nonatomic, strong) NSMutableArray *dataArr;

@end

@implementation SDHSportsSubNewsVC

- (id)init {
    if (self = [super init]) {
        isRefresh = NO;
        pageNum = 1;
        _dataArr = [NSMutableArray array];
    }
    
    return self;
}

- (void)getContentType {
    NSString *title = [self.tabInfoDic objectForKey:@"title"];
    if ([title caseInsensitiveCompare:@"News"] == NSOrderedSame) {
        contentType = 1117;
    } else if ([title caseInsensitiveCompare:@"Hot"] == NSOrderedSame) {
        contentType = 187;
    } else if ([title caseInsensitiveCompare:@"League"] == NSOrderedSame) {
        contentType = 192;
    } else if ([title caseInsensitiveCompare:@"expert"] == NSOrderedSame) {
        contentType = 913;
    } else if ([title caseInsensitiveCompare:@"NBA"] == NSOrderedSame) {
        contentType = 190;
    }
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor greenColor];
    [self getContentType];
    
    UITableView *table = [UITableView new];
    table.dataSource = self;
    table.delegate = self;
//    table.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:table];
    self.newsTable = table;
    
    UIView *view = [UIView new];
    self.newsTable.tableFooterView = view;
    
    __weak typeof(self) weakSelf = self;
    [self.newsTable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.view);
        make.right.equalTo(weakSelf.view);
        make.top.equalTo(weakSelf.view);
        make.bottom.equalTo(weakSelf.view.mas_bottom).offset(100);
    }];
    
    self.newsTable.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf headerRefreshing];
    }];
    self.newsTable.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [weakSelf footerRefreshing];
    }];
    
    [self loadData];
}

- (void)headerRefreshing {
    isRefresh = YES;
    pageNum = 1;
    
    [self loadData];
}

- (void)footerRefreshing
{
    pageNum ++;
    isRefresh = NO;
    [self loadData];
}

- (void)endLoading {
    [self.newsTable.mj_header endRefreshing];
    [self.newsTable.mj_footer endRefreshing];
    [MBProgressHUD hideHUD];
    
    [self.newsTable reloadData];
}

- (void)parseData:(NSDictionary *)dic {
    NSArray *arrayData = [SDHSportsNewsModal mj_objectArrayWithKeyValuesArray:dic[@"data"]];
    if (isRefresh) {
        [_dataArr removeAllObjects];
    }
    
    [_dataArr addObjectsFromArray:arrayData];
    [[SDHSportsDataManager sharedManager] addNews:arrayData];
}

- (void)loadData {
    SDHSportsRequest *request = [[SDHSportsRequest alloc] init];
    request.requestUrl = @"http://jsuywu8.com/guojitiyu/Tiyu1/getNews";
    request.requestPara = @{@"type":@(contentType), @"page":@(pageNum)};
    
    __weak typeof(self) weakSelf = self;
    [MBProgressHUD showLoading];
    [[SDHSportsNetRequest sharedRequest] sendRequest:request returnValueBlock:^(id  _Nonnull obj) {
        [weakSelf parseData:obj];
        
        [weakSelf endLoading];
    } errorBlock:^(id  _Nonnull obj) {
        NSLog(@"errorBlock: %@", obj);
        
        [weakSelf endLoading];
    } failureBlock:^{
        [weakSelf endLoading];
    }];
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
}

- (void)viewDidLayoutSubviews {
    
}

#pragma mark - tableview
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSInteger count = self.dataArr.count;
    return count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    return 90;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SDHSportsHomeNewsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[SDHSportsHomeNewsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    
    SDHSportsNewsModal *modal = [self.dataArr objectAtIndex:indexPath.row];
    [cell updateContent:modal];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    SDHSportsNewsModal *model = [self.dataArr objectAtIndex:indexPath.row];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"newsDetailNoti" object:model];
    
    [[SDHSportsDataManager sharedManager] addBrowseNews:model];
}


@end
